﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using Hr.Model;

namespace HR.HrHelper
{
    public class ModelHelper
    {
        /// <summary>
        /// 判断两个对象是否相当，常用于更新操作
        /// </summary>
        /// <param name="o1"></param>
        /// <param name="o2"></param>
        /// <returns></returns>
        public static bool ReflectModel(object o1, object o2)
        {
            Type t1 = o1.GetType();
            Type t2 = o2.GetType();
            string str1 = "";
            string str2 = "";
            foreach (PropertyInfo item in t1.GetProperties())
            {
                string s = item.GetType().ToString();
                str1+=GetModelValue(item.Name,o1);
            }
            foreach (PropertyInfo item in t2.GetProperties())
            {
                    str2 += GetModelValue(item.Name, o2);
                
            }

            if (str1.Equals(str2))
            {
                return true;
            }
            return false;
        }
        private static string GetModelValue(string FieldName, object obj)
        {
            try
            {
                Type Ts = obj.GetType();
                object o = Ts.GetProperty(FieldName).GetValue(obj, null);
                string Value = Convert.ToString(o);
                if (string.IsNullOrEmpty(Value)) return null;
                if (Value.IndexOf(".") != -1)
                {
                    Value= Value.Split('.')[0].ToString();
                }
                return Value;
            }
            catch
            {
                return null;
            }
        }
    }
}